home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / DebugGraphics.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  33.1 KB  |  941 lines  |  [TEXT/CWIE]

  1. // DebugGraphics.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8. import java.awt.image.FilteredImageSource;
  9. import java.awt.image.ImageProducer;
  10.  
  11. /** Graphics subclass supporting graphics debugging. Overrides most methods
  12.   * from Graphics. You rarely create a DebugGraphics, instead a View creates
  13.   * them in response to use of its <b>setDebugGraphics()</b> method, which
  14.   * registers that View, and its subviews, for graphics debugging.
  15.   * @see View#setDebugGraphics
  16.   */
  17. public class DebugGraphics extends Graphics {
  18.     static ExternalWindow       debugWindow;
  19.     /** Log graphics operations. */
  20.     public static final int     LOG_OPTION   = 1 << 0;
  21.     /** Flash graphics operations. */
  22.     public static final int     FLASH_OPTION = 1 << 1;
  23.     /** Show buffered operations in an ExternalWindow. */
  24.     public static final int     BUFFERED_OPTION = 1 << 2;
  25.     /** Don't debug graphics operations. */
  26.     public static final int     NONE_OPTION = -1;
  27.  
  28.  
  29.     /** Constructs a DebugGraphics for <b>view</b>. */
  30.     public DebugGraphics(View view) {
  31.         super(view);
  32.         setDebugOptions(view.shouldDebugGraphics());
  33.     }
  34.  
  35.     /** Constructs a DebugGraphics for <b>aBitmap</b>. */
  36.     public DebugGraphics(Bitmap aBitmap) {
  37.         super(aBitmap);
  38.     }
  39.  
  40.     /** Sets the Color used to flash drawing operations.
  41.       */
  42.     public static void setFlashColor(Color flashColor) {
  43.         info().flashColor = flashColor;
  44.     }
  45.  
  46.     /** Returns the Color used to flash drawing operations.
  47.       * @see #setFlashColor
  48.       */
  49.     public static Color flashColor() {
  50.         return info().flashColor;
  51.     }
  52.  
  53.     /** Sets the time delay of drawing operation flashing.
  54.       */
  55.     public static void setFlashTime(int flashTime) {
  56.         info().flashTime = flashTime;
  57.     }
  58.  
  59.     /** Returns the time delay of drawing operation flashing.
  60.       * @see #setFlashTime
  61.       */
  62.     public static int flashTime() {
  63.         return info().flashTime;
  64.     }
  65.  
  66.     /** Sets the number of times that drawing operations will flash.
  67.       */
  68.     public static void setFlashCount(int flashCount) {
  69.         info().flashCount = flashCount;
  70.     }
  71.  
  72.     /** Returns the number of times that drawing operations will flash.
  73.       * @see #setFlashCount
  74.       */
  75.     public static int flashCount() {
  76.         return info().flashCount;
  77.     }
  78.  
  79.     /** Sets the stream to which the DebugGraphics logs drawing operations.
  80.       */
  81.     public static void setLogStream(java.io.PrintStream stream) {
  82.         info().stream = stream;
  83.     }
  84.  
  85.     /** Returns the stream to which the DebugGraphics logs drawing operations.
  86.       * @see #setLogStream
  87.       */
  88.     public static java.io.PrintStream logStream() {
  89.         return info().stream;
  90.     }
  91.  
  92.     /** Creates an entry in the Graphics state stack. Each call to
  93.       * <b>pushState()</b> should be paired with a call to <b>popState()</b>.
  94.       * Any changes to the Graphics object between the calls will be flushed
  95.       * after the <b>popState()</b>.
  96.       * @see #popState
  97.       */
  98.     public void pushState() {
  99.         if (state().debugLog()) {
  100.             info().log(toShortString() + " -> state");
  101.         }
  102.         super.pushState();
  103.     }
  104.  
  105.     /** Restores the Graphics object to its condition before the most recent
  106.       * <b>pushState()</b> call. All fonts, colors, clipping rectangles and
  107.       * translations will be restored.
  108.       */
  109.     public void popState() {
  110.         if (state().debugLog()) {
  111.             info().log(toShortString() + " <- state");
  112.         }
  113.         super.popState();
  114.     }
  115.  
  116.     /** Sets the Font used for text drawing operations.
  117.       */
  118.     public void setFont(Font aFont) {
  119.         if (state().debugLog()) {
  120.             info().log(toShortString() + " Setting font: " + aFont);
  121.         }
  122.         super.setFont(aFont);
  123.     }
  124.  
  125.     /** Sets the color to be used for drawing and filling lines and shapes.
  126.       */
  127.     public void setColor(Color aColor) {
  128.         if (state().debugLog()) {
  129.             info().log(toShortString() + " Setting color: " + aColor);
  130.         }
  131.         super.setColor(aColor);
  132.     }
  133.  
  134.     /** Alters the coordinate system so that all drawing, filling, and
  135.       * clipping operations implicitly have <b>x</b> and <b>y</b> added to
  136.       * their positions. If there is any current translation, the total
  137.       * translation is the sum of the old and new translations.
  138.       * @see #xTranslation
  139.       * @see #yTranslation
  140.       * @see #translation
  141.       */
  142.     public void translate(int x, int y) {
  143.         GraphicsState state = state();
  144.  
  145.         if (state.debugLog()) {
  146.             info().log(toShortString() +
  147.                 " Translating by: " + new Point(x, y) +
  148.                 " to: " + new Point(x + state.xOffset, y + state.yOffset));
  149.         }
  150.         super.translate(x, y);
  151.     }
  152.  
  153.     /** Sets the rectangle within which drawing can occur.  Any drawing
  154.       * occurring outside of this rectangle will not appear. If
  155.       * <b>intersect</b> is <b>true</b>, this method intersects
  156.       * <b>rect</b> with the current clipping rectangle to produce the new
  157.       * clipping rectangle.
  158.       */
  159.     public void setClipRect(Rect rect, boolean intersect) {
  160.         GraphicsState state = state();
  161.  
  162.         super.setClipRect(rect, intersect);
  163.  
  164.         if (state().debugLog()) {
  165.             info().log(toShortString() +
  166.                 " Setting clipRect: " + rect +
  167.                 " New clipRect: " + clipRect());
  168.         }
  169.     }
  170.  
  171.     /** Sets the paint mode to overwrite the destination with the current
  172.       * color.
  173.       * @see #setXORMode
  174.       */
  175.     public void setPaintMode() {
  176.         if (state().debugLog()) {
  177.             info().log(toShortString() + " Setting paint mode");
  178.         }
  179.         super.setPaintMode();
  180.     }
  181.  
  182.     /** Sets the paint mode to alternate between the current color and
  183.       * <b>aColor</b>.
  184.       * @see #setPaintMode
  185.       */
  186.     public void setXORMode(Color aColor) {
  187.         if (state().debugLog()) {
  188.             info().log(toShortString() + " Setting XOR mode: " + aColor);
  189.         }
  190.         super.setXORMode(aColor);
  191.     }
  192.  
  193.     /** Draws the rectangle
  194.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>) using
  195.       * the current color.
  196.       */
  197.     public void drawRect(int x, int y, int width, int height) {
  198.         DebugGraphicsInfo info = info();
  199.         GraphicsState state = state();
  200.  
  201.         if (state.debugLog()) {
  202.             info().log(toShortString() +
  203.                       " Drawing rect: " +
  204.                       new Rect(x, y, width, height));
  205.         }
  206.  
  207.         if (isDrawingBuffer()) {
  208.             if (state.debugBuffered()) {
  209.                 Graphics debugGraphics = debugGraphics();
  210.  
  211.                 debugGraphics.drawRect(x, y, width, height);
  212.                 debugGraphics.dispose();
  213.             }
  214.         } else if (state.debugFlash()) {
  215.             Color oldColor = state.color;
  216.             int i, count = (info.flashCount * 2) - 1;
  217.  
  218.             for (i = 0; i < count; i++) {
  219.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  220.                 super.drawRect(x, y, width, height);
  221.                 AWTCompatibility.awtToolkit().sync();
  222.                 sleep(info.flashTime);
  223.             }
  224.             super.setColor(oldColor);
  225.         }
  226.         super.drawRect(x, y, width, height);
  227.     }
  228.  
  229.     /** Fills the rectangle
  230.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>) using the current
  231.       * color.
  232.       */
  233.     public void fillRect(int x, int y, int width, int height) {
  234.         DebugGraphicsInfo info = info();
  235.         GraphicsState state = state();
  236.  
  237.         if (state.debugLog()) {
  238.             info().log(toShortString() +
  239.                       " Filling rect: " +
  240.                       new Rect(x, y, width, height));
  241.         }
  242.  
  243.         if (isDrawingBuffer()) {
  244.             if (state.debugBuffered()) {
  245.                 Graphics debugGraphics = debugGraphics();
  246.  
  247.                 debugGraphics.fillRect(x, y, width, height);
  248.                 debugGraphics.dispose();
  249.             }
  250.         } else if (state.debugFlash()) {
  251.             Color oldColor = state.color;
  252.             int i, count = (info.flashCount * 2) - 1;
  253.  
  254.             for (i = 0; i < count; i++) {
  255.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  256.                 super.fillRect(x, y, width, height);
  257.                 AWTCompatibility.awtToolkit().sync();
  258.                 sleep(info.flashTime);
  259.             }
  260.             super.setColor(oldColor);
  261.         }
  262.         super.fillRect(x, y, width, height);
  263.     }
  264.  
  265.     /** Draws a rectangle with rounded corners in the rectangle
  266.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>), as determined by
  267.       * <b>arcWidth</b> and <b>arcHeight</b>.
  268.       */
  269.     public void drawRoundedRect(int x, int y, int width, int height,
  270.                                 int arcWidth, int arcHeight) {
  271.         DebugGraphicsInfo info = info();
  272.         GraphicsState state = state();
  273.  
  274.         if (state.debugLog()) {
  275.             info().log(toShortString() +
  276.                       " Drawing rounded rect: " +
  277.                       new Rect(x, y, width, height) +
  278.                       " arcWidth: " + arcWidth +
  279.                       " archHeight: " + arcHeight);
  280.         }
  281.         if (isDrawingBuffer()) {
  282.             if (state.debugBuffered()) {
  283.                 Graphics debugGraphics = debugGraphics();
  284.  
  285.                 debugGraphics.drawRoundedRect(x, y, width, height,
  286.                                               arcWidth, arcHeight);
  287.                 debugGraphics.dispose();
  288.             }
  289.         } else if (state.debugFlash()) {
  290.             Color oldColor = state.color;
  291.             int i, count = (info.flashCount * 2) - 1;
  292.  
  293.             for (i = 0; i < count; i++) {
  294.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  295.                 super.drawRoundedRect(x, y, width, height,
  296.                                       arcWidth, arcHeight);
  297.                 AWTCompatibility.awtToolkit().sync();
  298.                 sleep(info.flashTime);
  299.             }
  300.             super.setColor(oldColor);
  301.         }
  302.         super.drawRoundedRect(x, y, width, height, arcWidth, arcHeight);
  303.     }
  304.  
  305.     /** Fills a rectangle with rounded corners in the rectangle
  306.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>), as determined by
  307.       * <b>arcWidth</b> and <b>arcHeight</b>.
  308.       */
  309.     public void fillRoundedRect(int x, int y, int width, int height,
  310.                                 int arcWidth, int arcHeight) {
  311.         DebugGraphicsInfo info = info();
  312.         GraphicsState state = state();
  313.  
  314.         if (state.debugLog()) {
  315.             info().log(toShortString() +
  316.                       " Drawing rounded rect: " +
  317.                       new Rect(x, y, width, height) +
  318.                       " arcWidth: " + arcWidth +
  319.                       " archHeight: " + arcHeight);
  320.         }
  321.         if (isDrawingBuffer()) {
  322.             if (state.debugBuffered()) {
  323.                 Graphics debugGraphics = debugGraphics();
  324.  
  325.                 debugGraphics.fillRoundedRect(x, y, width, height,
  326.                                               arcWidth, arcHeight);
  327.                 debugGraphics.dispose();
  328.             }
  329.         } else if (state.debugFlash()) {
  330.             Color oldColor = state.color;
  331.             int i, count = (info.flashCount * 2) - 1;
  332.  
  333.             for (i = 0; i < count; i++) {
  334.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  335.                 super.fillRoundedRect(x, y, width, height,
  336.                                       arcWidth, arcHeight);
  337.                 AWTCompatibility.awtToolkit().sync();
  338.                 sleep(info.flashTime);
  339.             }
  340.             super.setColor(oldColor);
  341.         }
  342.         super.fillRoundedRect(x, y, width, height, arcWidth, arcHeight);
  343.     }
  344.  
  345.     /** Draws a line from the point (<b>x1</b>, <b>y1</b>) to the
  346.       * point (<b>x2</b>, <b>y2</b>) in the current color.
  347.       */
  348.     public void drawLine(int x1, int y1, int x2, int y2) {
  349.         DebugGraphicsInfo info = info();
  350.         GraphicsState state = state();
  351.  
  352.         if (state.debugLog()) {
  353.             info().log(toShortString() +
  354.                       " Drawing line: " +
  355.                       new Rect(x1, y1, x2, y2));
  356.         }
  357.  
  358.         if (isDrawingBuffer()) {
  359.             if (state.debugBuffered()) {
  360.                 Graphics debugGraphics = debugGraphics();
  361.  
  362.                 debugGraphics.drawLine(x1, y1, x2, y2);
  363.                 debugGraphics.dispose();
  364.             }
  365.         } else if (state.debugFlash()) {
  366.             Color oldColor = state.color;
  367.             int i, count = (info.flashCount * 2) - 1;
  368.  
  369.             for (i = 0; i < count; i++) {
  370.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  371.                 super.drawLine(x1, y1, x2, y2);
  372.                 AWTCompatibility.awtToolkit().sync();
  373.                 sleep(info.flashTime);
  374.             }
  375.             super.setColor(oldColor);
  376.         }
  377.         super.drawLine(x1, y1, x2, y2);
  378.     }
  379.  
  380.     /** Draws the point (<b>x</b>, <b>y</b>) in the current color.
  381.       */
  382.     public void drawPoint(int x, int y) {
  383.         DebugGraphicsInfo info = info();
  384.         GraphicsState state = state();
  385.  
  386.         if (state.debugLog()) {
  387.             info().log(toShortString() +
  388.                       " Drawing point: " +
  389.                       new Point(x, y));
  390.         }
  391.         if (isDrawingBuffer()) {
  392.             if (state.debugBuffered()) {
  393.                 Graphics debugGraphics = debugGraphics();
  394.  
  395.                 debugGraphics.drawLine(x, y, x, y);
  396.                 debugGraphics.dispose();
  397.             }
  398.         } else if (state.debugFlash()) {
  399.             Color oldColor = state.color;
  400.             int i, count = (info.flashCount * 2) - 1;
  401.  
  402.             for (i = 0; i < count; i++) {
  403.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  404.                 super.drawLine(x, y, x, y);
  405.                 AWTCompatibility.awtToolkit().sync();
  406.                 sleep(info.flashTime);
  407.             }
  408.             super.setColor(oldColor);
  409.         }
  410.         super.drawLine(x, y, x, y);
  411.     }
  412.  
  413.     /** Draws an oval inside the rect
  414.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>) in the
  415.       * current color.
  416.       */
  417.     public void drawOval(int x, int y, int width, int height) {
  418.         DebugGraphicsInfo info = info();
  419.         GraphicsState state = state();
  420.  
  421.         if (state.debugLog()) {
  422.             info().log(toShortString() +
  423.                       " Drawing oval: " +
  424.                       new Rect(x, y, width, height));
  425.         }
  426.         if (isDrawingBuffer()) {
  427.             if (state.debugBuffered()) {
  428.                 Graphics debugGraphics = debugGraphics();
  429.  
  430.                 debugGraphics.drawOval(x, y, width, height);
  431.                 debugGraphics.dispose();
  432.             }
  433.         } else if (state.debugFlash()) {
  434.             Color oldColor = state.color;
  435.             int i, count = (info.flashCount * 2) - 1;
  436.  
  437.             for (i = 0; i < count; i++) {
  438.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  439.                 super.drawOval(x, y, width, height);
  440.                 AWTCompatibility.awtToolkit().sync();
  441.                 sleep(info.flashTime);
  442.             }
  443.             super.setColor(oldColor);
  444.         }
  445.         super.drawOval(x, y, width, height);
  446.     }
  447.  
  448.     /** Fills an oval inside the rect
  449.       * (<b>x</b>, <b>y</b>, <b>width</b>, <b>height</b>) in the
  450.       * current color.
  451.       */
  452.     public void fillOval(int x, int y, int width, int height) {
  453.         DebugGraphicsInfo info = info();
  454.         GraphicsState state = state();
  455.  
  456.         if (state.debugLog()) {
  457.             info().log(toShortString() +
  458.                       " Filling oval: " +
  459.                       new Rect(x, y, width, height));
  460.         }
  461.         if (isDrawingBuffer()) {
  462.             if (state.debugBuffered()) {
  463.                 Graphics debugGraphics = debugGraphics();
  464.  
  465.                 debugGraphics.fillOval(x, y, width, height);
  466.                 debugGraphics.dispose();
  467.             }
  468.         } else if (state.debugFlash()) {
  469.             Color oldColor = state.color;
  470.             int i, count = (info.flashCount * 2) - 1;
  471.  
  472.             for (i = 0; i < count; i++) {
  473.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  474.                 super.fillOval(x, y, width, height);
  475.                 AWTCompatibility.awtToolkit().sync();
  476.                 sleep(info.flashTime);
  477.             }
  478.             super.setColor(oldColor);
  479.         }
  480.         super.fillOval(x, y, width, height);
  481.     }
  482.  
  483.     /** Draws an arc in <b>aRect</b> from <b>startAngle</b> for
  484.       * <b>arcAngle</b> degrees.
  485.       */
  486.     public void drawArc(int x, int y, int width, int height,
  487.                         int startAngle, int arcAngle) {
  488.         DebugGraphicsInfo info = info();
  489.         GraphicsState state = state();
  490.  
  491.         if (state.debugLog()) {
  492.             info().log(toShortString() +
  493.                       " Drawing arc: " +
  494.                       new Rect(x, y, width, height) +
  495.                       " startAngle: " + startAngle +
  496.                       " arcAngle: " + arcAngle);
  497.         }
  498.         if (isDrawingBuffer()) {
  499.             if (state.debugBuffered()) {
  500.                 Graphics debugGraphics = debugGraphics();
  501.  
  502.                 debugGraphics.drawArc(x, y, width, height,
  503.                                       startAngle, arcAngle);
  504.                 debugGraphics.dispose();
  505.             }
  506.         } else if (state.debugFlash()) {
  507.             Color oldColor = state.color;
  508.             int i, count = (info.flashCount * 2) - 1;
  509.  
  510.             for (i = 0; i < count; i++) {
  511.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  512.                 super.drawArc(x, y, width, height, startAngle, arcAngle);
  513.                 AWTCompatibility.awtToolkit().sync();
  514.                 sleep(info.flashTime);
  515.             }
  516.             super.setColor(oldColor);
  517.         }
  518.         super.drawArc(x, y, width, height, startAngle, arcAngle);
  519.     }
  520.  
  521.     /** Fills an arc in <b>aRect</b> from <b>startAngle</b> for
  522.       * <b>arcAngle</b> degrees.
  523.       */
  524.     public void fillArc(int x, int y, int width, int height,
  525.                         int startAngle, int arcAngle) {
  526.         DebugGraphicsInfo info = info();
  527.         GraphicsState state = state();
  528.  
  529.         if (state.debugLog()) {
  530.             info().log(toShortString() +
  531.                       " Filling arc: " +
  532.                       new Rect(x, y, width, height) +
  533.                       " startAngle: " + startAngle +
  534.                       " arcAngle: " + arcAngle);
  535.         }
  536.         if (isDrawingBuffer()) {
  537.             if (state.debugBuffered()) {
  538.                 Graphics debugGraphics = debugGraphics();
  539.  
  540.                 debugGraphics.fillArc(x, y, width, height,
  541.                                       startAngle, arcAngle);
  542.                 debugGraphics.dispose();
  543.             }
  544.         } else if (state.debugFlash()) {
  545.             Color oldColor = state.color;
  546.             int i, count = (info.flashCount * 2) - 1;
  547.  
  548.             for (i = 0; i < count; i++) {
  549.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  550.                 super.fillArc(x, y, width, height, startAngle, arcAngle);
  551.                 AWTCompatibility.awtToolkit().sync();
  552.                 sleep(info.flashTime);
  553.             }
  554.             super.setColor(oldColor);
  555.         }
  556.         super.fillArc(x, y, width, height, startAngle, arcAngle);
  557.     }
  558.  
  559.     /** Draws the polygon described by <b>xPoints</b> and <b>yPoints</b>
  560.       * using the current color. Both arrays must have at least
  561.       * <b>nPoints</b> integers.
  562.       */
  563.     public void drawPolygon(int xPoints[], int yPoints[], int nPoints) {
  564.         DebugGraphicsInfo info = info();
  565.         GraphicsState state = state();
  566.  
  567.         if (state.debugLog()) {
  568.             info().log(toShortString() +
  569.                       " Drawing polygon: " +
  570.                       " nPoints: " + nPoints +
  571.                       " X's: " + xPoints +
  572.                       " Y's: " + yPoints);
  573.         }
  574.         if (isDrawingBuffer()) {
  575.             if (state.debugBuffered()) {
  576.                 Graphics debugGraphics = debugGraphics();
  577.  
  578.                 debugGraphics.drawPolygon(xPoints, yPoints, nPoints);
  579.                 debugGraphics.dispose();
  580.             }
  581.         } else if (state.debugFlash()) {
  582.             Color oldColor = state.color;
  583.             int i, count = (info.flashCount * 2) - 1;
  584.  
  585.             for (i = 0; i < count; i++) {
  586.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  587.                 super.drawPolygon(xPoints, yPoints, nPoints);
  588.                 AWTCompatibility.awtToolkit().sync();
  589.                 sleep(info.flashTime);
  590.             }
  591.             super.setColor(oldColor);
  592.         }
  593.         super.drawPolygon(xPoints, yPoints, nPoints);
  594.     }
  595.  
  596.     /** Fills the polygon described by <b>xPoints</b> and <b>yPoints</b>
  597.       * using the current color. Both arrays must have at least
  598.       * <b>nPoints</b> integers.
  599.       */
  600.     public void fillPolygon(int xPoints[], int yPoints[], int nPoints) {
  601.         DebugGraphicsInfo info = info();
  602.         GraphicsState state = state();
  603.  
  604.         if (state.debugLog()) {
  605.             info().log(toShortString() +
  606.                       " Filling polygon: " +
  607.                       " nPoints: " + nPoints +
  608.                       " X's: " + xPoints +
  609.                       " Y's: " + yPoints);
  610.         }
  611.         if (isDrawingBuffer()) {
  612.             if (state.debugBuffered()) {
  613.                 Graphics debugGraphics = debugGraphics();
  614.  
  615.                 debugGraphics.fillPolygon(xPoints, yPoints, nPoints);
  616.                 debugGraphics.dispose();
  617.             }
  618.         } else if (state.debugFlash()) {
  619.             Color oldColor = state.color;
  620.             int i, count = (info.flashCount * 2) - 1;
  621.  
  622.             for (i = 0; i < count; i++) {
  623.                 super.setColor((i % 2) == 0 ? info.flashColor : oldColor);
  624.                 super.fillPolygon(xPoints, yPoints, nPoints);
  625.                 AWTCompatibility.awtToolkit().sync();
  626.                 sleep(info.flashTime);
  627.             }
  628.             super.setColor(oldColor);
  629.         }
  630.         super.fillPolygon(xPoints, yPoints, nPoints);
  631.     }
  632.  
  633.     /** Displays <b>bitmap</b> at the point (<b>x</b>, <b>y</b>).
  634.       */
  635.     public void drawBitmapAt(Bitmap bitmap, int x, int y) {
  636.         DebugGraphicsInfo info = info();
  637.         GraphicsState state = state();
  638.  
  639.         if (state.debugLog()) {
  640.             info().log(toShortString() +
  641.                       " Drawing bitmap: " + bitmap +
  642.                       " at: " + new Point(x, y));
  643.         }
  644.  
  645.         if (isDrawingBuffer()) {
  646.             if (state.debugBuffered()) {
  647.                 Graphics debugGraphics = debugGraphics();
  648.  
  649.                 debugGraphics.drawBitmapAt(bitmap, x, y);
  650.                 debugGraphics.dispose();
  651.             }
  652.         } else if (state.debugFlash()) {
  653.             int i, count = (info.flashCount * 2) - 1;
  654.             ImageProducer oldProducer
  655.                 = AWTCompatibility.awtImageProducerForBitmap(bitmap);
  656.             ImageProducer newProducer
  657.                 = new FilteredImageSource(oldProducer,
  658.                                 new DebugGraphicsColorFilter(info.flashColor));
  659.             Bitmap newBitmap =
  660.                     AWTCompatibility.bitmapForAWTImageProducer(newProducer);
  661.  
  662.             for (i = 0; i < count; i++) {
  663.                 super.drawBitmapAt((i % 2) == 0 ? newBitmap : bitmap, x, y);
  664.                 AWTCompatibility.awtToolkit().sync();
  665.                 sleep(info.flashTime);
  666.             }
  667.         }
  668.         super.drawBitmapAt(bitmap, x, y);
  669.     }
  670.  
  671.     /** Draws <b>bitmap</b> at the point (<b>x</b>, <b>y</b>), scaled in the
  672.       * X-dimension to have width <b>width</b> and the Y-dimension to
  673.       * have height <b>height</b>.
  674.       */
  675.     public void drawBitmapScaled(Bitmap bitmap, int x, int y,
  676.                                  int width, int height) {
  677.         DebugGraphicsInfo info = info();
  678.         GraphicsState state = state();
  679.  
  680.         if (state.debugLog()) {
  681.             info().log(toShortString() +
  682.                       " Drawing scaled bitmap: " + bitmap +
  683.                       " in rect: " + new Rect(x, y, width, height));
  684.         }
  685.  
  686.         if (isDrawingBuffer()) {
  687.             if (state.debugBuffered()) {
  688.                 Graphics debugGraphics = debugGraphics();
  689.  
  690.                 debugGraphics.drawBitmapScaled(bitmap, x, y, width, height);
  691.                 debugGraphics.dispose();
  692.             }
  693.         } else if (state.debugFlash()) {
  694.             int i, count = (info.flashCount * 2) - 1;
  695.             ImageProducer oldProducer
  696.                 = AWTCompatibility.awtImageProducerForBitmap(bitmap);
  697.             ImageProducer newProducer
  698.                 = new FilteredImageSource(oldProducer,
  699.                                 new DebugGraphicsColorFilter(info.flashColor));
  700.             Bitmap newBitmap =
  701.                     AWTCompatibility.bitmapForAWTImageProducer(newProducer);
  702.  
  703.             for (i = 0; i < count; i++) {
  704.                 super.drawBitmapScaled((i % 2) == 0 ? newBitmap : bitmap,
  705.                                        x, y, width, height);
  706.                 AWTCompatibility.awtToolkit().sync();
  707.                 sleep(info.flashTime);
  708.             }
  709.  
  710.         }
  711.         super.drawBitmapScaled(bitmap, x, y, width, height);
  712.     }
  713.  
  714.     /** Draws <b>aString</b> at the point (<b>x</b>, <b>y</b>) using the
  715.       * current font and color.
  716.       * @see #drawStringInRect
  717.       */
  718.     public void drawString(String aString, int x, int y) {
  719.         DebugGraphicsInfo info = info();
  720.         GraphicsState state = state();
  721.  
  722.         if (state.debugOptions != 0) {
  723.             if (state.font == null || !state.font.wasDownloaded()) {
  724.                 if (state.debugLog()) {
  725.                     info().log(toShortString() +
  726.                                 " Drawing string: \"" + aString +
  727.                                 "\" at: " + new Point(x, y));
  728.                 }
  729.  
  730.                 if (isDrawingBuffer()) {
  731.                     if (state.debugBuffered()) {
  732.                         Graphics debugGraphics = debugGraphics();
  733.  
  734.                         debugGraphics.drawString(aString, x, y);
  735.                         debugGraphics.dispose();
  736.                     }
  737.                 } else if (state.debugFlash()) {
  738.                     Color oldColor = state.color;
  739.                     int i, count = (info.flashCount * 2) - 1;
  740.  
  741.                     for (i = 0; i < count; i++) {
  742.                         super.setColor((i % 2) == 0 ? info.flashColor
  743.                                                     : oldColor);
  744.                         super.drawString(aString, x, y);
  745.                         AWTCompatibility.awtToolkit().sync();
  746.                         sleep(info.flashTime);
  747.                     }
  748.                     super.setColor(oldColor);
  749.                 }
  750.             }
  751.         }
  752.         super.drawString(aString, x, y);
  753.     }
  754.  
  755.     /** Draws <b>length</b> bytes of <b>data</b>, beginning <b>offset</b> bytes
  756.       * into the array, using the current font and color, at the point
  757.       * (<b>x</b>, <b>y</b>).
  758.       */
  759.     public void drawBytes(byte data[], int offset, int length, int x, int y) {
  760.         DebugGraphicsInfo info = info();
  761.         GraphicsState state = state();
  762.  
  763.         if (state.debugOptions != 0) {
  764.             Font font = font();
  765.  
  766.             if (font == null || !font.wasDownloaded()) {
  767.                 if (state.debugLog()) {
  768.                     info().log(toShortString() +
  769.                               " Drawing bytes at: " + new Point(x, y));
  770.                 }
  771.  
  772.                 if (isDrawingBuffer()) {
  773.                     if (state.debugBuffered()) {
  774.                         Graphics debugGraphics = debugGraphics();
  775.  
  776.                         debugGraphics.drawBytes(data, offset, length, x, y);
  777.                         debugGraphics.dispose();
  778.                     }
  779.                 } else if (state.debugFlash()) {
  780.                     Color oldColor = state.color;
  781.                     int i, count = (info.flashCount * 2) - 1;
  782.  
  783.                     for (i = 0; i < count; i++) {
  784.                         super.setColor((i % 2) == 0 ? info.flashColor
  785.                                                     : oldColor);
  786.                         super.drawBytes(data, offset, length, x, y);
  787.                         AWTCompatibility.awtToolkit().sync();
  788.                         sleep(info.flashTime);
  789.                     }
  790.                     super.setColor(oldColor);
  791.                 }
  792.             }
  793.         }
  794.         super.drawBytes(data, offset, length, x, y);
  795.     }
  796.  
  797.     /** Draws <b>length</b> characters of <b>data</b>, beginning <b>offset</b>
  798.       * characters into the array, using the current font and color, at the
  799.       * point (<b>x</b>, <b>y</b>).
  800.       */
  801.     public void drawChars(char data[], int offset, int length, int x, int y) {
  802.         DebugGraphicsInfo info = info();
  803.         GraphicsState state = state();
  804.  
  805.         if (state.debugOptions != 0) {
  806.             Font font = font();
  807.  
  808.             if (font == null || !font.wasDownloaded()) {
  809.                 if (state.debugLog()) {
  810.                     info().log(toShortString() +
  811.                               " Drawing chars at " +  new Point(x, y));
  812.                 }
  813.  
  814.                 if (isDrawingBuffer()) {
  815.                     if (state.debugBuffered()) {
  816.                         Graphics debugGraphics = debugGraphics();
  817.  
  818.                         debugGraphics.drawChars(data, offset, length, x, y);
  819.                         debugGraphics.dispose();
  820.                     }
  821.                 } else if (state.debugFlash()) {
  822.                     Color oldColor = state.color;
  823.                     int i, count = (info.flashCount * 2) - 1;
  824.  
  825.                     for (i = 0; i < count; i++) {
  826.                         super.setColor((i % 2) == 0 ? info.flashColor
  827.                                                     : oldColor);
  828.                         super.drawChars(data, offset, length, x, y);
  829.                         AWTCompatibility.awtToolkit().sync();
  830.                         sleep(info.flashTime);
  831.                     }
  832.                     super.setColor(oldColor);
  833.                 }
  834.             }
  835.         }
  836.         super.drawChars(data, offset, length, x, y);
  837.     }
  838.  
  839.     void copyArea(int x, int y, int width, int height, int destX, int destY) {
  840.         if (state().debugLog()) {
  841.             info().log(toShortString() +
  842.                       " Copying area from: " +
  843.                       new Rect(x, y, width, height) +
  844.                       " to: " + new Point(destX, destY));
  845.         }
  846.         super.copyArea(x, y, width, height, destX, destY);
  847.     }
  848.  
  849.     final void sleep(int mSecs) {
  850.         try {
  851.             Thread.sleep(mSecs);
  852.         } catch (Exception e) {
  853.         }
  854.     }
  855.  
  856.     /** Enables/disables diagnostic information about every graphics
  857.       * operation. The value of <b>debug</b> indicates how this information
  858.       * should be displayed. DEBUG_LOG causes a text message to be printed.
  859.       * DEBUG_FLASH causes the drawing to flash several times. DEBUG_BUFFERED
  860.       * creates an ExternalWindow that shows each operation on an
  861.       * offscreen buffer. The value of <b>debug</b> is bitwise OR'd into
  862.       * the current value. To disable debugging use DEBUG_NONE.
  863.       */
  864.     public void setDebugOptions(int debugOptions) {
  865.         if (debugOptions != 0) {
  866.             GraphicsState state = state();
  867.  
  868.             if (debugOptions == NONE_OPTION) {
  869.                 if (state.debugOptions != 0) {
  870.                     System.err.println(toShortString() + " Disabling debug");
  871.                     state.debugOptions = 0;
  872.                 }
  873.             } else {
  874.                 if (state.debugOptions != debugOptions) {
  875.                    state.debugOptions |= debugOptions;
  876.                    if (state.debugLog()) {
  877.                        System.err.println(toShortString() + " Enabling debug");
  878.                    }
  879.                }
  880.            }
  881.         }
  882.     }
  883.  
  884.     /** Returns the current debugging options.
  885.       * @see setDebugOptions
  886.       */
  887.     public int debugOptions() {
  888.         return state().debugOptions;
  889.     }
  890.  
  891.     void setDebug(View view) {
  892.         setDebugOptions(viewDebug(view));
  893.     }
  894.  
  895.     /** Returns the state of graphics debugging.
  896.       * @see #setDebug
  897.       */
  898.     public int debug() {
  899.         return state().debugOptions;
  900.     }
  901.  
  902.     private Graphics debugGraphics() {
  903.         Graphics                debugGraphics;
  904.         DebugGraphicsInfo       info = info();
  905.         GraphicsState           state = state();
  906.         Size                    windowSize;
  907.         Rect                    windowBounds;
  908.         ExternalWindow          debugWindow;
  909.  
  910.         if (info.debugWindow == null) {
  911.             info.debugWindow = new ExternalWindow();
  912.             info.debugWindow.setResizable(false);
  913.         }
  914.         debugWindow = info.debugWindow;
  915.         windowSize = debugWindow.windowSizeForContentSize(
  916.                             primaryClipRect.width, primaryClipRect.height);
  917.         windowBounds = debugWindow.bounds();
  918.         if (windowBounds.width > windowSize.width) {
  919.             windowSize.width = windowBounds.width;
  920.         }
  921.         if (windowBounds.height > windowSize.height) {
  922.             windowSize.height = windowBounds.height;
  923.         }
  924.         debugWindow.sizeTo(windowSize.width, windowSize.height);
  925.         debugWindow.show();
  926.         debugGraphics = new DebugGraphics(debugWindow.rootView());
  927.         debugGraphics.setFont(state.font);
  928.         debugGraphics.setColor(state.color);
  929.         debugGraphics.translate(state.xOffset, state.yOffset);
  930.         debugGraphics.setClipRect(clipRect());
  931.         if (state.debugFlash()) {
  932.             debugGraphics.setDebugOptions(FLASH_OPTION);
  933.         }
  934.         return debugGraphics;
  935.     }
  936.  
  937.     static DebugGraphicsInfo info() {
  938.         return Graphics.info();
  939.     }
  940. }
  941.